package com.vancouver.knows.business.tracker;

import java.util.ArrayList;
import java.util.List;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.CityDiscountDetailInfo;
import com.vancouver.knows.business.bean.CityNewsDetailInfo;
import com.vancouver.knows.business.bean.EventDetailInfo;
import com.vancouver.knows.business.bean.EventInfo;
import com.vancouver.knows.business.bean.PostFollowerUserInfo;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.business.bean.UserPostInfo;
import com.vancouver.knows.business.task.mark.UserFollowTaskMark;

/**
 * 用户关注
 * @author chenxunlin01
 * 
 */
public class UserFollowTracker extends ABInvokeTracker {

	public UserFollowTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	public String TAG() {
		return UserFollowTracker.class.getSimpleName();
	}

	@Override
	public void handleResult(OperateResult result) {
		updateFollowStatus(result, false);
	}

	private void updateFollowStatus(OperateResult result, boolean isFollowed) {
		UserFollowTaskMark taskMark = (UserFollowTaskMark) result.getTaskMark();

		CityNewsDetailInfo cityNewsDetailInfo = module.getCacheManager().getCityNewsDetailInfoMap()
				.get(taskMark.getPostId());
		if (cityNewsDetailInfo != null) {
			cityNewsDetailInfo.setUserFollowed("Y");
			if (!isFollowed) {
				cityNewsDetailInfo.setFollowCount(cityNewsDetailInfo.getFollowCount() + 1);
			}
		}

		EventDetailInfo eventDetailInfo = module.getCacheManager().getEventDetailInfoMap().get(taskMark.getPostId());
		if (eventDetailInfo != null) {
			eventDetailInfo.setUserFollowed("Y");
			UserInfo userInfo = module.getCacheManager().getUserInfo();
			PostFollowerUserInfo userEventDetailUserInfo = new PostFollowerUserInfo();
			userEventDetailUserInfo.setName(userInfo.getName());
			userEventDetailUserInfo.setId(userInfo.getUserID());
			userEventDetailUserInfo.setAvatar(userInfo.getAvatar());
			List<PostFollowerUserInfo> data = eventDetailInfo.getEventDetailUserInfoList();
			if (data == null) {
				data = new ArrayList<PostFollowerUserInfo>();
			}

			if (!isExit(data, userInfo.getUserID())) {
				data.add(userEventDetailUserInfo);
				if (!isFollowed) {
					eventDetailInfo.setFollowCount(eventDetailInfo.getFollowCount() + 1);
				}
			}
		}

		CityDiscountDetailInfo cityDiscountDetailInfo = module.getCacheManager().getDiscountDetailInfoMap()
				.get(taskMark.getPostId());
		if (cityDiscountDetailInfo != null) {
			cityDiscountDetailInfo.setUserFollowed("Y");
			if (!isFollowed) {
				cityDiscountDetailInfo.setFollowCount(cityDiscountDetailInfo.getFollowCount() + 1);
			}
		}

		UserPostInfo userPostInfo = module.getCacheManager().getUserPostsInfoCache().getItemInfoById(taskMark.getPostId());
		if (userPostInfo != null) {
			userPostInfo.setUserFollowed("Y");
			userPostInfo.incrementFollowCount();
		}

		imContext.handleMobEmptyMessage(Constants.BUSINESS_STATUS_USER_FOLLOW);
	}

	private boolean isExit(List<PostFollowerUserInfo> data, String id) {
		for (PostFollowerUserInfo eventDetailUserInfo : data) {
			if (eventDetailUserInfo.getId() != null && eventDetailUserInfo.getId().equals(id)) {
				return true;
			}
		}

		return false;
	}

	@Override
	public void handleFault(OperateResult result) {
		ActionException actionException = result.getActionException();
		if (actionException.getExCode() == 4) {
			updateFollowStatus(result, true);
		}
	}
}
